package dealgb28175

import (
	"context"
	"core/db"
	"core/server/baseserver"
	"fmt"
	"iotall/iotcore/api/proxyup"
	"iotall/iotdeal/deal/basedeal"

	"iotall/iotcore/entity"
)

type Deal6001Msg struct {
	SeqNo    uint16
	Ver      string
	Dt       string
	Gw       string
	Len      uint16
	Cmd      byte
	Data     []byte
	Check    bool
	CheckErr string
}
type Deal6001 struct {
	basedeal.KeepAliveDeal
}

func (p *Deal6001) Start() {
	p.KeepAliveDeal.ChildDeal = p.ChildDeal
	p.KeepAliveDeal.Start()

}

func (p *Deal6001) Splits(data []byte) *[]Deal6001Msg {
	var r = make([]Deal6001Msg, 0)
	//40 40 47 BD 0B 02 0F 24 0E 1A 02 18 00 00 00 00 00 00 01 00 00 00 00 00 00 00 07 8E 23 23
	for index := 0; index < len(data); {
		var gwup = p.Split(data, index)
		if gwup.Check {
			r = append(r, *gwup)
			index += len(gwup.Data)
		} else {
			r = make([]Deal6001Msg, 0)
			r = append(r, *gwup)
			break
		}
	}
	return &r
}
func (p *Deal6001) Split(data []byte, index int) *Deal6001Msg {
	var r = &Deal6001Msg{
		SeqNo:    0,
		Ver:      "",
		Dt:       "",
		Gw:       "",
		Len:      0,
		Cmd:      0,
		Data:     nil,
		Check:    false,
		CheckErr: "",
	}
	//40 40 47 BD 0B 02 0F 24 0E 1A 02 18 00 00 00 00 00 00 01 00 00 00 00 00 00 00 07 8E 23 23
	if index+30 > len(data) {
		r.CheckErr = "长度不够"
		r.Data = data
		return r
	}
	r.Len = uint16(256) * uint16((data)[index+25])
	r.Len += uint16((data)[index+24])
	r.Data = (data)[index : index+30+int(r.Len)]
	if 0x40 != r.Data[0] || 0x40 != r.Data[1] {
		r.CheckErr = "协议头错误"
	}
	if 0x23 != r.Data[len(r.Data)-1] || 0x23 != r.Data[len(r.Data)-2] {
		r.CheckErr = "协议尾错误"
	}

	r.SeqNo = uint16(256) * uint16(r.Data[3])
	r.SeqNo += uint16(r.Data[2])

	r.Ver = fmt.Sprintf("%v.%v", r.Data[5], r.Data[4])

	r.Dt = fmt.Sprintf("20%v-%v-%v %v:%v:%v", r.Data[11], r.Data[10], r.Data[9], r.Data[8], r.Data[7], r.Data[6])

	r.Gw = fmt.Sprintf("%v.%v.%v.%v.%v.%v", r.Data[17], r.Data[16], r.Data[15], r.Data[14], r.Data[13], r.Data[12])

	r.Cmd = r.Data[26]

	r.Check = true
	return r
}

func (p *Deal6001) ChildDeal(ctx context.Context, in *proxyup.UpReq) (*proxyup.UpResp, error) {

	var lpackage = p.Splits(in.Data)
	var r = &proxyup.UpResp{
		Ok:   true,
		Msg:  "",
		Data: nil,
		P1:   "",
		P2:   "",
		P3:   "",
		P4:   "",
	}
	var package0 = (*lpackage)[0]

	if !package0.Check {
		r.Ok = false
		r.Msg = package0.CheckErr

	} else {

		r.Ok = true
		r.Sid = package0.Gw

		var mpk = basedeal.NewStdPkg(r.Sid, in.ReqId, in.CommPortId, in.CommProxyId)
		mpk.PhysicalState = "在线"
		mpk.ClientId = in.RemoteAddr

		for _, v := range *lpackage {
			switch v.Cmd {
			case 7:
				r.Msg += "心跳 "
			}
		}
		r.Data = in.Data

		basedeal.PushStdPkg(mpk)

	}

	if r.Ok && len(r.Data) > 0 {
		r.DownMsg = "确认"
	}
	return r, nil
}
func init() {
	var cp = &entity.CommPort{Id: 295937834576130048}
	var tx = db.DbMaster.Model(cp).Find(cp)
	if tx.Error == nil {
		var deal = &Deal6001{
			basedeal.KeepAliveDeal{
				CommPort:   cp,
				BaseServer: baseserver.BaseServer{Interval: 5000, Name: cp.Name + "处理程序"},
			},
		}
		basedeal.AddDeal(deal)

	}
}
